$(function () {
    // 配置路径
    var pageUrl = config.urlAnaConfig.testPath;
    // 获取Cookie
    function getCookie(name){
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
    //       console.log(arrcookie);
        //遍历匹配
        for ( var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
                if (arr[0] ==name){
                    return arr[1];
                }
        }
        return "";
    }
    var name = "tokenval";
    // tiken值
    var cookie_value = getCookie(name);
    console.log(cookie_value);
    var name1 = "userName";
    // 用户名值
    var cookie_value1 = getCookie(name1);
    $(".userName").html(cookie_value1);
    $(".nav-usename").html(cookie_value1);
    // 天数限制
    $(".xianzhi").keyup(function () {

    });


/*    // 下拉数据
    $.ajax({
        url: pageUrl+'/api/dailyactivity/getplatforms',
        type: 'POST',
        dataType: 'json',
        headers: {
            Authorization: 'Bearer '+cookie_value
        },
        success: function (data) {
            var msg = '';
            if (data) {
                if (data.code === 200) {
    //                  店铺名称
                    var details1 = data.data.platforms;
                    console.log(details1);
                    var details1F = $(".details11");
                    var mydetails1 = details1;
                    /!*for in 循环*!/
                     for(var i in mydetails1){
                         var text="<option value='" + mydetails1[i] + "'>" + mydetails1[i] + " </option>";
                         details1F.append(text);
                     };
                }
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });
    $('.details11').change(function(){
        $(".details22 option:gt(0)").remove();
        $(".details22").attr("disabled",false);
        var details1D= $(this).val();
        console.log(details1D);
        $.ajax({
            url: pageUrl+'/api/dailyactivity/getstorebyplatform',
            type: 'POST',
            dataType: 'json',
            data:{
                platform:details1D
            },
            headers: {
                Authorization: 'Bearer '+cookie_value
            },
            success: function (data) {
                var msg = '';
                if (data) {
                    if (data.code === 200) {
        //                  店铺名称
                        var details2 = data.data.stores;
                        console.log(details2);
                        var details2F = $(".details22");
                        var mydetails2 = details2;
                        /!*for in 循环*!/
                         for(var i in mydetails2){
                             var text="<option value='" + mydetails2[i] + "'>" + mydetails2[i] + " </option>";
                             details2F.append(text);
                         };

                    }
                }
            },
            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

        });
    });*/
    // 图表
    $.ajax({
        url: pageUrl+'/api/common/getaccesstoken',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            var msg = '';
            if (data) {
                var accessToken = data.data.accessToken.value;
                
                console.log('accessToken');
                var myUrl = "https://app.powerbi.cn/reportEmbed?reportId=0c221c7c-6529-4ec0-8191-8b73242d88fc&groupId=13e42e94-3404-46d8-b605-228c35984df3";
                 cldb(accessToken,myUrl);
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });

    // 确定
    // 图形重置
/*    var pingjieIndex1 = 0;
    $(".application").on("click",function () {
        var details1Data = $(".details11").val();
        var detai2s1Data = $(".details22").val();
        if (details1Data == 0 || detai2s1Data==0){
            alert("请选择完整选项");
            return false;
        };
        // 拼接图形盒子1
        pingjieIndex1++;
        var tu1Id = "reportContainer";
        tu1Id = tu1Id + pingjieIndex1;
        console.log(tu1Id);
        var arrtx1 = [];
        arrtx1.push("<div class='tu11 no-border'" + "id=" +tu1Id +">");
        arrtx1.push("</div>");
        str1 = arrtx1.join('');
        $(".tu1").append(str1);
        // 删除其他元素
        $('.tu1'+' #'+tu1Id).siblings().remove();

        var url1 = "https://app.powerbi.cn/reportEmbed?reportId=0c221c7c-6529-4ec0-8191-8b73242d88fc&groupId=13e42e94-3404-46d8-b605-228c35984df3";
        url1 = url1+"&filter=platform_table/code eq"+" '"+details1Data+"' "+"and store_table/code eq "+"'"+detai2s1Data+"'";
        $.ajax({
            url: pageUrl+'/api/common/getaccesstoken',
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                var msg = '';
                if (data) {
                    var accessToken = data.data.accessToken.value;
                    rchd(accessToken,url1);
                }
            },
            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }
        });
    });*/


    function cldb(accessToken,myUrl) {
        // Read embed application token from Model
        var accessToken = accessToken
        // Read embed URL from Model
        var embedUrl = myUrl;
        console.log(embedUrl);
        // Read report Id from Model
        var embedReportId = "4169dad8-05ee-4c50-9266-ddded496dba6";
        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;
        // Embed configuration used to describe the what and how to embed.
        // This object is used when calling powerbi.embed.
        // This also includes settings and options such as filters.
        // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
        var config = {
            type: 'report',
            // tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId,
            viewMode: models.ViewMode.View,
            // filters: filter,
            permissions: models.Permissions.All ,
            settings: {
                filterPaneEnabled: false,
                navContentPaneEnabled: false
            }
        };
        // Get a reference to the embedded report HTML element
        var tu11 = $('.tu11')[0];
        if ("" != "") {
            $("#RLS").prop('checked', true);
            $("#RLSdiv").show();
        }
        else
        {
            $("#RLS").prop('checked', false);
            $("#RLSdiv").hide();
        }
        if ("False" == "True") {
            $("#noRLSdiv").hide();
            $("#RLS").removeAttr("disabled");
            $("#RLS").change(function () {
                if ($(this).is(":checked")) {
                    $("#RLSdiv").show(300);
                } else {
                    $("#RLSdiv").hide(200);
                }
            });
        }
        else
        {
            $("#noRLSdiv").show();
        }
        var report = powerbi.embed(tu11, config);
    };

});